<?php
namespace app\admin\controller;

use app\common\model\Kapian as KapianModel;
use app\common\model\Nav as NavModel;
use app\common\model\User as UserModel;
use app\common\controller\AdminBase;
use think\Db;
use think\Session;

class Kapian extends AdminBase
{
	protected $kapian_model;
	protected $nav_model;
	protected $user_model;

	protected function _initialize()
	{
		parent::_initialize();
		$this->kapian_model = new KapianModel();
		$this->nav_model = new NavModel();
		$this->user_model = new UserModel();

		$nav_list = $this->nav_model->where('status',1)->column('name','id');
		$user_list = $this->user_model->where('status',1)->column('name','id');

		$this->assign('nav_list',$nav_list);
		$this->assign('user_list',$user_list);
	}

	public function index($keyword = '', $page = 1)
	{
		$map = [];
		if($keyword){
			$map['name|phone'] = ['like' , '%{$keyword}%'];
		}
		$kapian_list = $this->kapian_model->where($map)->order('id DESC')->paginate(15,false,['page' => $page]);

		return $this->fetch('index',['kapian_list'=>$kapian_list,'keyword'=>$keyword]);
	}

	public function reading($id)
	{
		$kapian = $this->kapian_model->find($id);

		return $this->fetch('reading',['kapian' => $kapian]);
	}

	public function delete($id = 0, $ids = [])
	{
		$id = $ids ? array_merge($ids) : $id;
		if($id){
			if($this->kapian_model->destroy($id)){
				return $this->success('删除成功');
			}else{
				return $this->error('删除失败');
			}
		}else{
			return $this->error('请选择需要删除的内容');
		}
	}
}